package com.hustzp.com.xichuangzhu.poetry;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

/**
 * Created by Administrator on 2016/6/15.
 */
public class AddWorkErrataActivity extends XCZBaseFragmentActivity {

    private EditText workErrata;
    private Button submitBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_work_errata);

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        TextView navTxt = (TextView) findViewById(R.id.tv_nav_txt);
        navTxt.setText("勘误记录");

        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText("添加勘误");
    }

    private void initViews() {
        workErrata = (EditText) findViewById(R.id.tv_work_errata);

        // 提交
        submitBtn = (Button) findViewById(R.id.submit_btn);
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(workErrata.getText().toString().trim())) {
                    DialogFactory.hintDialog(AddWorkErrataActivity.this, "勘误内容不可为空");
                    return;
                }

                AVObject recmdObj = new AVObject("WorkErrata");
                recmdObj.put("content", workErrata.getText().toString());
                recmdObj.put("user", AVUser.getCurrentUser());
                recmdObj.put("workId", getIntent().getIntExtra("workId", -1));
                recmdObj.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            setResult(RESULT_OK);
                            finish();
                        } else {
                            DialogFactory.hintDialog(AddWorkErrataActivity.this, "勘误内容提交失败，请重试");
                        }
                    }
                });
            }
        });
    }

}
